package com.example.safehome;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
import android.util.Log;

import org.litepal.LitePal;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class EditDataActivity extends AppCompatActivity {

    System system;
    EditText editTextPassword;
    EditText editTextConfirmPassword;
    EditText editTextPhoneNumber;
    EditText editTextFireNumber;
    EditText editTextDelayTime;
    Button btnEditData;
    String password;
    String confirmPassword;
    String phoneNumber;
    String fireNumber;
    Double delayTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_data);
        editTextPassword = (EditText) findViewById(R.id.update_password);
        editTextConfirmPassword = (EditText) findViewById(R.id.update_confirm_password);
        editTextPhoneNumber = (EditText) findViewById(R.id.update_phone_number);
        editTextFireNumber = (EditText) findViewById(R.id.update_fire_number);
        editTextDelayTime = (EditText) findViewById(R.id.update_delay_time);

        // 保存按钮事件
        btnEditData = (Button) findViewById(R.id.edit_save);
        btnEditData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 获取相应控件内的值
                password = editTextPassword.getText().toString();
                confirmPassword = editTextConfirmPassword.getText().toString();
                phoneNumber = editTextPhoneNumber.getText().toString();
                fireNumber = editTextFireNumber.getText().toString();
                if (editTextDelayTime.getText().toString() != null && !editTextDelayTime.getText().toString().equals("")) {
                    delayTime = Double.parseDouble(editTextDelayTime.getText().toString());
                }

                // 获取当前用户名
                LoginUser loginUser = new LoginUser();
                List<System> systemList = LitePal.where("hostName = ?", loginUser.getUsername()).find(System.class);
                system = (System) systemList.get(0);
                Log.d("systemUser", system.getHostName());

                // 数据库修改
                if (password.equals(confirmPassword)) {
                    if (password.length() != 0) {
                        system.setHostPassword(password);
                    }
                    List<Login> loginList = LitePal.where("username = ?", loginUser.getUsername()).find(Login.class);
                    Login login = loginList.get(0);
                    if (password.length() != 0) {
                        login.setPassword(password);
                    }
                    if (phoneNumber.length() != 0) {
                        system.setPhoneNumber(phoneNumber);
                    }
                    if (fireNumber.length() != 0) {
                        system.setFireCall(fireNumber);
                    }
                    if (editTextDelayTime.getText().toString() != null && !editTextDelayTime.getText().toString().equals("")) {
                        system.setDelayTime(delayTime);
                    }

                    // 保存数据
                    login.save();
                    system.save();
                    Toast.makeText(EditDataActivity.this, "保存成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(EditDataActivity.this, SystemActivity.class);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(EditDataActivity.this, "两次输入密码不一致", Toast.LENGTH_LONG).show();
                }
           }
        });
    }
}
